import { httpClient } from "../httpClient";
import { Bill } from "../../types/bill";

export interface UploadResponse {
    success: boolean;
    data: any;
}

export async function uploadBillImage(formData: FormData): Promise<UploadResponse> {
    return httpClient<UploadResponse>("/image/upload/", "POST", formData);
}

export async function submitBillData(bills: Bill[]): Promise<any> {
    const requestData = bills.map(bill => ({
        bill_date: bill.date,
        amount: bill.amount,
        category: bill.category,
        remark: bill.remark,
    }));
    return httpClient("/bills/batch", "POST", requestData);
}

export interface ApiBill {
    bill_date: string;
    amount: number;
    category: string;
    remark: string;
    id: number;
    created_at: string;
    updated_at: string;
}

export async function getBills(skip: number = 0, limit: number = 1000, year?: number, month?: number): Promise<ApiBill[]> {
    let url = `/bills/?skip=${skip}&limit=${limit}`;
    if (year !== undefined && month !== undefined) {
        url += `&year=${year}&month=${month}`;
    }
    return httpClient<ApiBill[]>(url, "GET");
}

export async function deleteBill(billId: number): Promise<any> {
    return httpClient(`/bills/${billId}`, "DELETE");
}

export async function updateBill(bill: Bill): Promise<any> {
    const requestData = {
        bill_date: bill.date,
        amount: bill.amount,
        category: bill.category,
        remark: bill.remark,
    };
    return httpClient(`/bills/${bill.id}`, "PUT", requestData);
}

export async function addBill(bill: { bill_date: string; amount: number; category: string; remark: string }): Promise<any> {
    return httpClient("/bills/", "POST", bill);
}